﻿using System;
using System.Collections.Generic;
using MyLib.IO;
using log4net;

namespace MyLib.Web {
	/// <summary>
	/// 
	/// </summary>
	public class WebApplicationRegisterFileTypesEventArgs : EventArgs {
		/// <summary>
		/// 
		/// </summary>
		public IDictionary<int, string> FileTypes { get; private set; }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fileTypes"></param>
		public WebApplicationRegisterFileTypesEventArgs(IDictionary<int, string> fileTypes) {
			FileTypes = fileTypes;
		}
	} // end of WebApplicationRegisterFileTypesEventArgs.

	/// <summary>
	/// 
	/// </summary>
	public class MyWebApplication : System.Web.HttpApplication {
		/// <summary>
		/// Internal logger.
		/// </summary>
		protected ILog m_logger = LogManager.GetLogger("Global");

		/// <summary>
		/// 
		/// </summary>
		public event EventHandler<WebApplicationRegisterFileTypesEventArgs> RegisterFileTypes;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void Application_Start(object sender, EventArgs e) {
			// Initialize log4net.
			log4net.Config.XmlConfigurator.Configure();

			var fileTypes = new Dictionary<int, string>();
			OnRegisterFileTypes(fileTypes);
			FileSystemProvider.RegisterFileTypes(fileTypes);

			System.Web.UI.WebControls.WebControl.DisabledCssClass = "disabled";

			OnStart();

			m_logger.Info("Application has started.");
		}

		/// <summary>
		/// 
		/// </summary>
		protected virtual void OnStart() {
		}

		/// <summary>
		/// 
		/// </summary>
		protected virtual void OnBeginRequest() {
		}

		/// <summary>
		/// 
		/// </summary>
		protected virtual void OnEnd() {
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fileTypes"></param>
		protected virtual void OnRegisterFileTypes(IDictionary<int, string> fileTypes) {
			if (RegisterFileTypes != null)
				RegisterFileTypes(this, new WebApplicationRegisterFileTypesEventArgs(fileTypes));
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void Application_End(object sender, EventArgs e) {
			OnEnd();

			m_logger.Info("Application has shutdown.");
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void Application_BeginRequest(object sender, EventArgs e) {
			OnBeginRequest();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void Application_Error(object sender, EventArgs e) {
			var msg = string.Empty;
			if (Request != null) {
				msg = string.Format("Unhandled exception when request url \"{0}\".", Request.RawUrl);
			} else {
				msg = "Unhandled exception.";
			}

			if (m_logger.IsErrorEnabled) {
				m_logger.Error(msg, Server.GetLastError());
			}
		}

	} // end of MyWebApplication.
}
